package cl.gustavoklagges.tarealistadocompras;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListaActivity extends Activity {
	
	private ListView lista;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lista);
        
        lista = (ListView)findViewById(R.id.listView1);
        
        String[] datosLista = mostrar();
        
        ArrayAdapter<String> adaptador = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, datosLista);
        
        lista.setAdapter(adaptador);
    
    }

    
    public void agregarProducto(View v){
    	Intent intento = new Intent(this,AgregarActivity.class);
    	startActivityForResult(intento, RESULT_OK);
    }
	
    
    public String[] mostrar() {
    	String[] nombres = {""};
		AdminSqLiteHelper admin = new AdminSqLiteHelper(this, "administracion",
				null, 1);

		SQLiteDatabase bd = admin.getWritableDatabase();
		//String rut = editRut.getText().toString();

		Cursor fila = bd.rawQuery("select nombre from producto", null);
		int cont = 0;
		while(fila.moveToNext()){
			nombres[cont]= fila.getString(0);
			fila.moveToNext();
			cont++;
		}
		/*
		if (fila.moveToFirst()) {
			editRut.setText(fila.getString(0));
			editNombre.setText(fila.getString(1));
			editDireccion.setText(fila.getString(2));
		} else{
			Toast.makeText(this, "No existe una persona con dicho Rut",Toast.LENGTH_SHORT).show();
		}	
		*/		
		bd.close();
		return nombres;
	}
}
